home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7782 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I modify a character string that's declared as an array of char?
  5. Date: Wed, 28 Feb 96 19:20:43 GMT
  6. Organization: none
  7. Message-ID: <825535243snz@genesis.demon.co.uk>
  8. References: <4gj2nl$840@mirzam.usc.edu> <4gjbjh$o0h@news-f.iadfw.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gjbjh$o0h@news-f.iadfw.net>
  15.            alpet@airmail.net "Adam Peterson" writes:
  16.  
  17. >awawda@mirzam.usc.edu (Abu A. Wawda) wrote:
  18. >
  19. >>How do I write a function that can modify a string that was declared
  20. >>as an array of characters?
  21. >
  22. >>for example:
  23. >
  24. >>myfunction(char **s)
  25. >>{
  26. >>       // modify string s
  27.  
  28. Since you are posting to comp.lang.c post valid C code. // may introduce a
  29. comment in C++ but it is a syntax error in C.
  30.  
  31. >>}
  32. >
  33. >>main()
  34. >>{
  35. >>   char mydata[50];
  36. >
  37. >>   strcpy(mydata,"test string");
  38. >>   myfunction(&mydata);
  39. >>}
  40.  
  41. myfunction takes a pointer to a pointer but this is trying to pass it a
  42. pointer to an array which is illegal in C (pointers and arrays are entirely
  43. different things). Your compiler should have generated a warning or error
  44. when compiling this.
  45.  
  46. >>this does not seem to work! it works fine if i pass i declare mydata
  47. >>as a pointer to a character like this (or dynamically allocate the
  48. >>string using a pointer) and pass the address of the pointer:
  49. >
  50. >>the problem with this is i need to write a function that will modify a
  51. >>string created with an array of characters. i can't figure out how do
  52. >>to this. please help. thanks!
  53. >
  54. >void myfunction(char *s)
  55. >{
  56. >        strcat(s,"more stuff"); // modify string s
  57. >}
  58. >
  59. >void main()
  60.  
  61. In C main returns int. Writing void main() gives the compiler the freedom to
  62. do anything it likes with the rest of the code - the whole program ceases
  63. to have any meaning at all.
  64.  
  65. >{
  66. >   char mydata[50];
  67. >
  68. >   strcpy(mydata,"test string");
  69. >   printf(mydata);
  70. >   myfunction(mydata);
  71. >   printf(mydata);
  72. >}
  73. >
  74. >The above does what I think you want to do.  When you call
  75. >'myfunction', the '&' was not neccessary by definition, then *in*
  76. >'myfunction', you were dropping two levels of redirection down.
  77.  
  78. I think you meant 'indirection'!
  79.  
  80. -- 
  81. -----------------------------------------
  82. Lawrence Kirby | fred@genesis.demon.co.uk
  83. Wilts, England | 70734.126@compuserve.com
  84. -----------------------------------------
  85.